<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/17 0017
  Time: 上午 10:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>编辑</title>
    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/common.css">
</head>
<body>

    <div class="wrap-padding-15">
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id" id="id" value="${voteItem.id}">
                    <input type="text" name="sortNo"  value="${voteItem.sortNo}" lay-verify="required|number" placeholder="请输入编号(数字)" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name"  value="${voteItem.name}" lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <input type="hidden" name="img" id="img" value="${voteItem.img}">
                        <div class="layui-upload-list">
                            <img style="width: 100px; height: 100px"  class="layui-upload-img" id="demo1" <c:if test="${not empty voteItem.img}"> src="/dl/img?path=${voteItem.img}" </c:if>>
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
           <%-- <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注：</label>
                <div class="layui-input-block">
                    <textarea name="detail" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>--%>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formSubmit">保存</button>
                    <a href="/vote" target="_parent"><button type="button" class="layui-btn layui-btn-primary">取消</button></a>
                </div>
            </div>
        </form>
    </div>


    <script src="/static/plugins/jquery.js"></script>
    <script src="/static/plugins/layui/layui.js"></script>

    <script>

        layui.use(['element','layer','form','upload','laydate'], function(){
            var form = layui.form,
             upload = layui.upload,
             laydate = layui.laydate,
             layer = layui.layer




            //监听提交
             form.on('submit(formSubmit)',function (data) {
                 $.ajax({
                     url:"/voteitem/update",
                     type:"post",
                     dataType:"json",
                     data:data.field,
                     success:function (rs) {
                         layer.msg(rs.message,{time:1000,icon:1},function () {
                             if(rs.code==1){
                                 parent.$("#tb_voteitem").bootstrapTable("refresh")
                                 var index = parent.layer.getFrameIndex(window.name)
                                 parent.layer.close(index)
                             }
                         })
                     }
                 })
                 return false
             })





            laydate.render({
                elem: '#time'
                ,type: 'datetime'
                ,range:'~'
            });


            var uploadInst = upload.render({
                elem: '#test1'
                ,url: '/upload'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    /*  if(res.code > 0){
                     return layer.msg('上传失败');
                     }*/
                    $("#img").val(res[0].src);

                    //上传成功
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });




        });


    </script>
</body>
</html>

